package slw.controls
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextFieldAutoSize;
	
	import slw.events.ScrollEvent;
	
	/**
	 * TextArea
	 * 类
	 * @author ddx<br/>
	 * 2011-8-8
	 *
	 */
	public class TextArea extends QTextField
	{
		//
		private var _vScrollBar:ScrollBar;
		private var _hScrollBar:ScrollBar;
		
		private var _scrolled:Boolean=false;
		/**
		 * 构造函数
		 * 
		 */
		public function TextArea(text:String="")
		{
			super(text);
			
			init();
			
			this.multiline=true;
			this.editabled=true;
		}
		
		/*
		-----------------------------------
		setters getters
		-----------------------------------
		*/
		//
		override public function set width(value:Number):void{
			if(this.width==value) return;
			
			super.width=value;
			
			textFieldChange(new Event(Event.CHANGE));
			updateDisplayList();
		}
		//
		override public function set height(value:Number):void{
			if(this.height==value) return;
			
			super.height=value;
			
			textFieldChange(new Event(Event.CHANGE));
			updateDisplayList();
		}
		/*
		-----------------------------------
		public  methods
		-----------------------------------
		*/
		
		/*
		-----------------------------------
		private  methods
		-----------------------------------
		*/
		//
		private function init():void{
			this.textField.border=true;
			
			addListeners();
		}
		//添加侦听
		private function addListeners():void{
			this.textField.addEventListener(Event.CHANGE,textFieldChange);
			this.textField.addEventListener(Event.SCROLL,textFieldScroll);
		}
		//删除侦听
		private function removeListeners():void{
			
		}
		//
		private function updateDisplayList():void{
			var $padding:int=2;
			if(_vScrollBar!=null&&_vScrollBar.visible){
				this.textField.width=this.width-this.padding*3-_vScrollBar.width-$padding;
				_vScrollBar.x=this.width-_vScrollBar.width-$padding;
				_vScrollBar.y=this.textField.y;
			}else{
				this.textField.width=this.width-this.padding*$padding;
			}
			if(_hScrollBar!=null&&_hScrollBar.visible){
				this.textField.height=this.height-this.padding*3-_hScrollBar.height-$padding;
				_hScrollBar.y=this.height-_hScrollBar.height-$padding;
				_hScrollBar.x=this.textField.x;
			}else{
				this.textField.height=this.height-this.padding*2;
			}
		}
		//
		private function updateScrollBar():void{
			if(_vScrollBar!=null&&_vScrollBar.visible){
				_vScrollBar.height=this.height-this.padding*2;
				_vScrollBar.pageSize=this.textField.bottomScrollV-this.textField.scrollV;
				_vScrollBar.minScrollPosition=1;
				_vScrollBar.maxScrollPosition=this.textField.numLines;
			}
			if(_hScrollBar!=null&&_hScrollBar.visible){
				_hScrollBar.width=this.width-this.padding*2;
				_hScrollBar.pageSize=this.textField.width;
				_hScrollBar.minScrollPosition=0;
				_hScrollBar.maxScrollPosition=this.textField.maxScrollH+_hScrollBar.pageSize;
			}
			if(_vScrollBar!=null&&_vScrollBar.visible&&_hScrollBar!=null&&_hScrollBar.visible){
				_vScrollBar.height=this.height-this.padding*2-_hScrollBar.height;
				_hScrollBar.width=this.height-this.padding*2-_vScrollBar.width;
			}
		}
		//
		private function updateMaxScrollPosition():void{
			if(_vScrollBar!=null&&_vScrollBar.visible){
				_vScrollBar.pageSize=this.textField.bottomScrollV-this.textField.scrollV;
				_vScrollBar.maxScrollPosition=this.textField.numLines;
			}
			if(_hScrollBar!=null&&_hScrollBar.visible){
				_hScrollBar.pageSize=this.textField.width;
				_hScrollBar.maxScrollPosition=this.textField.maxScrollH+_hScrollBar.pageSize;
			}
		}
		//
		private function textFieldChange(e:Event):void{
				if(this.textField.textHeight>this.textField.height){
					addScrollBar("vertical");
				}else{
					hideScrollBar("vertical");
				}
				if(this.textField.textWidth>this.textField.width){
					addScrollBar("horizontal");
				}else{
					hideScrollBar("horizontal");
				}
			
			updateMaxScrollPosition();
		}
		//
		private function textFieldScroll(e:Event):void{
			if(_scrolled) return;
			
			if(_vScrollBar!=null&&_vScrollBar.visible)
				_vScrollBar.scrollPercent=(this.textField.scrollV-1)/(this.textField.maxScrollV-1);
			if(_hScrollBar!=null&&_hScrollBar.visible)
				_hScrollBar.scrollPercent=this.textField.scrollH/this.textField.maxScrollH;
		}
		//
		private function scrollHandler(e:ScrollEvent):void{
			_scrolled=true;
			
			if(e.target==_vScrollBar){
				this.textField.scrollV=e.scrollPercent*this.textField.maxScrollV;
			}else if(e.target==_hScrollBar){
				this.textField.scrollH=e.scrollPercent*this.textField.maxScrollH;
			}
			
			if(this.stage!=null)
				this.stage.addEventListener(MouseEvent.MOUSE_UP,stageMouseUp);
		}
		//鼠标弹起
		private function stageMouseUp(e:MouseEvent):void{
			_scrolled=false;
			
			if(this.stage!=null){
				this.stage.removeEventListener(MouseEvent.MOUSE_UP,stageMouseUp);
			}
		}
		//
		private function addScrollBar(direction:String):void{
			if(direction=="vertical"){
				if(_vScrollBar==null){
					_vScrollBar=new ScrollBar();
					this.buildContainer.addChild(_vScrollBar);
					
					_vScrollBar.addEventListener(ScrollEvent.SCROLL,scrollHandler);
				}else{
					_vScrollBar.visible=true;
				}
			}else if(direction=="horizontal"){
				if(_hScrollBar==null){
					_hScrollBar=new ScrollBar(ScrollBar.HORIZONTAL);
					this.buildContainer.addChild(_hScrollBar);
					
					_hScrollBar.addEventListener(ScrollEvent.SCROLL,scrollHandler);
				}else{
					_hScrollBar.visible=true;
				}
			}
			
			updateDisplayList();
			updateScrollBar();
		}
		//
		private function hideScrollBar(direction:String):void{
			if(direction=="vertical"){
				if(_vScrollBar!=null) _vScrollBar.visible=false;
			}else if(direction=="horizontal"){
				if(_hScrollBar!=null) _hScrollBar.visible=false;
			}
			
			updateDisplayList();
		}
	}
}